<?php

namespace app\swoole\service;

use think\swoole\Server;
use think\facade\Env;
use  think\facade\Log;

class Service
{

    // 事件回调定义
    public function onOpen($server, $request)
    {
        $get = $request->get;
        $data=[
            'token'=>$get['token']??'',
            'source'=>$get['source']??''
        ];
        if (isset($request->get['token']) && Event::checkToken($request->fd,$data, $server)) {
            echo "server: checkToken success with fd{$request->fd}\n";
        } else {
            Log::error("异常请求:" . json_encode($request));
            echo "server: checkToken error with fd{$request->fd}\n";
        }
    }

    public function onMessage($server, $frame)
    {
        try {
            Log::info('WebSocket请求开始，请求信息[' . json_encode($frame) . ']');
            $data = json_decode($frame->data, true);
            $cmd = $data['cmd'];
            $param=[
                'token'=>$data['token']??'',
                'source'=>$data['source']??''
            ];
            $userInfo = Event::checkToken($frame->fd, $param, $server);
            //用户没认证过
            if (!$userInfo) {
                return false;
            }
            //心跳
            if ($cmd == 'heartbeat') {
                $server->push($frame->fd, Event::returnJson());
                return false;
            }
            $resut = Event::$cmd($frame->fd, $userInfo, $data, $server);
            //$server->push($frame->fd, $resut['data']);
        } catch (\Error $er) {
            Log::error('WebSocket请求异常,异常信息：' . $er->getMessage() . '错误地址：' . $er->getFile() . $er->getLine());
            $res = ['code' => $er->getCode(), 'msg' => $er->getMessage(), 'data' => '', 'cmd' => ''];
        } catch (\Exception $era) {
            Log::error('WebSocket请求异常,异常信息：' . $era->getMessage() . '错误地址：' . $era->getFile() . $era->getLine());
            $res = ['code' => $era->getCode(), 'msg' => $era->getMessage(), 'data' => '', 'cmd' => ''];
        } catch (\ErrorException $ere) {
            Log::error('WebSocket请求异常,异常信息：' . $ere->getMessage() . '错误地址：' . $ere->getFile() . $ere->getLine());
            $res = ['code' => $ere->getCode(), 'msg' => $ere->getMessage(), 'data' => '', 'cmd' => ''];
        }
        if (isset($resut)) {
            $server->push($frame->fd, $resut);
        }

    }

    public function onRequest($request, $response)
    {
        $response->end("<h1>Hello Swoole. #" . rand(1000, 9999) . "</h1>");
    }

    public function onClose($server, $fd)
    {
        try {
            Log::info('WebSocket关闭请求开始，请求信息[' . json_encode($server) . ']');
            $resut = Event::disconnect($fd, $server);
            echo "client {$fd} closed\n";
        } catch (\Error $er) {
            Log::error('WebSocket请求异常,异常信息：' . $er->getMessage() . '错误地址：' . $er->getFile() . $er->getLine());
        } catch (\Exception $era) {
            Log::error('WebSocket请求异常,异常信息：' . $era->getMessage() . '错误地址：' . $era->getFile() . $era->getLine());
        } catch (\ErrorException $ere) {
            Log::error('WebSocket请求异常,异常信息：' . $ere->getMessage() . '错误地址：' . $ere->getFile() . $ere->getLine());
        }

    }


}